﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

using InsuranceCompany.Helpers;
using InsuranceCompany.Core.Domain.Objects;
using ClientNS = InsuranceCompany.Core.Domain.Client;

namespace InsuranceCompany.Client
{
    public partial class Clients : InsuranceForm, InsuranceMainModuleInterface
    {
        private ClientNS.Client SelectedClient;
        private ObjectsCollection ClientsAll;
        public Clients()
        {
            InitializeComponent();
        }
        public Clients(ref List<ObjectsCollection> objects)
        {
            InitializeComponent();
            this.Objects = objects;
            this.ClientsAll = this.Objects.Find(x => x.GetName() == "Clients");
            this.UpdateItemsListView(this.ClientsAll.Items);

        }
        private void AddItemClick(object sender, EventArgs e)
        {
            List<ObjectsCollection> objects = this.Objects;
            Edit editForm = new Edit(ref objects);  
            this.Hide();
            editForm.ShowDialog();
            this.Objects = objects;
            this.Show();
            this.UpdateItemsListView(this.ClientsAll.Items);
        }
        private void UpdateItemsListView(List<Entity> clients)
        {
            this.ClientsListView.Items.Clear();
            foreach (var item in clients)
            {
                string clientType = item.GetType().Name;
                ListViewItem listViewItem = new ListViewItem();
                if (clientType == "NaturalClient")
                {
                    ClientNS.NaturalClient client = (ClientNS.NaturalClient)item;
                    listViewItem.SubItems[0].Text = client.GetId().ToString();
                    listViewItem.SubItems.Add(client.Name);
                    listViewItem.SubItems.Add(client.Surname);
                    listViewItem.SubItems.Add(client.Phone);
                    listViewItem.SubItems.Add(client.Email);
                    listViewItem.SubItems.Add("0");
                    listViewItem.SubItems.Add("0");
                }
                else if (clientType == "JuridicalClient")
                {
                    ClientNS.JuridicalClient client = (ClientNS.JuridicalClient)item;
                    listViewItem.SubItems[0].Text = client.GetId().ToString();
                    listViewItem.SubItems.Add(client.CompanyName);
                    listViewItem.SubItems.Add(client.Name + " " + client.Surname);
                    listViewItem.SubItems.Add(client.Phone);
                    listViewItem.SubItems.Add(client.Email);
                    listViewItem.SubItems.Add("0");
                    listViewItem.SubItems.Add("0");
                }
                this.ClientsListView.Items.Add(listViewItem);
            }
        }
        private void StatisticButton_Click(object sender, EventArgs e)
        {
            ClientReportInfo clientReportInfo = new ClientReportInfo();
            this.Hide();
            clientReportInfo.ShowDialog();
            this.Show();
        }
        private void EnabledChangeDataButtons(bool enable)
        {
            this.EditClient.Enabled = enable;
            this.DeleteClient.Enabled = enable;
        }
        private void ItemsListViewItemSelectionChanged(object sender, ListViewItemSelectionChangedEventArgs e)
        {
            if (this.ClientsListView.SelectedItems.Count == 1)
            {
                this.EnabledChangeDataButtons(true);
                ListView.SelectedListViewItemCollection selectedItem = this.ClientsListView.SelectedItems;
                int id = -1;
                foreach (ListViewItem item in selectedItem)
                {
                    id = Int32.Parse(item.SubItems[0].Text);
                    break;
                }
                this.SelectedClient = (ClientNS.Client)this.ClientsAll.Items.Find(x => x.GetId() == id);
            }
            else
                this.EnabledChangeDataButtons(false);
        }

        private void DeleteItemClick(object sender, EventArgs e)
        {
            this.ClientsAll.Items.Remove(this.SelectedClient);
            this.UpdateItemsListView(this.ClientsAll.Items);
        }

        private void EditItemClick(object sender, EventArgs e)
        {
            List<ObjectsCollection> objects = this.Objects;
            Edit editForm = new Edit(ref objects, this.SelectedClient);
            this.Hide();
            editForm.ShowDialog();
            this.Objects = objects;
            this.Show();
            this.UpdateItemsListView(this.ClientsAll.Items);
        }

        private void SearchButtonClick(object sender, EventArgs e)
        {
            if (this.SearchTextBox.Text != "")
            {
                string words = this.SearchTextBox.Text;
                List<Entity> result = new List<Entity>();
                foreach (Entity item in this.ClientsAll.Items)
                {
                    string clientType = item.GetType().Name;
                    if (clientType == "NaturalClient")
                    {
                        ClientNS.NaturalClient client = (ClientNS.NaturalClient)item;
                        if ( (words.Contains(client.Name) && client.Name != "") ||
                            (words.Contains(client.Surname) && client.Surname != "") ||
                            (words.Contains(client.Patronymic) && client.Patronymic != "")
                            )
                            result.Add(client); 
                    }
                    else if (clientType == "JuridicalClient")
                    {
                        ClientNS.JuridicalClient client = (ClientNS.JuridicalClient)item;
                        if ((words.Contains(client.Name) && client.Name != "") ||
                            (words.Contains(client.Surname) && client.Surname != "") ||
                            (words.Contains(client.Patronymic) && client.Patronymic != "") ||
                            (words.Contains(client.CompanyName) && client.CompanyName != "") || 
                            (words.Contains(client.Director.Name) && client.Director.Name != "") ||
                            (words.Contains(client.Accountant.Name) && client.Accountant.Name != "")
                            )
                            result.Add(client); 
                    }
                }
                this.UpdateItemsListView(result);
            }
        }

        private void ClearSearchButtonClick(object sender, EventArgs e)
        {
            this.UpdateItemsListView(this.ClientsAll.Items);
        }
    }
}
